﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmThongTinNhanVien : Form
    {
        public frmThongTinNhanVien()
        {
            InitializeComponent();
        }

        private NhanVienService nvService = new NhanVienService();
        private NhaHangService nhService = new NhaHangService();

        public int flag = 0;
        public bool fix;
        public bool lost = false;
        public bool dirty = false;

        public NhanVien nv = new NhanVien();

        private void LoadGioiTinh()
        {
            cb_gioitinh.Items.Add("Nam");
            cb_gioitinh.Items.Add("Nữ");
            cb_gioitinh.Text = cb_gioitinh.Items[0].ToString();
        }

        private void LoadLoaiNV()
        {
            DataTable dbtab = nvService.LayTatCaLoaiNV();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbdow = dbtab.Rows[i];
                cb_loainv.Items.Add(dbdow[1].ToString());
            }
            cb_loainv.Text = cb_loainv.Items[0].ToString();
        }

        private void LoadNH()
        {
            DataTable dbtab = nhService.LoadNH();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                cb_nh.Items.Add(dbrow["TenNH"].ToString());
            }
            cb_nh.Text = cb_nh.Items[0].ToString();
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            NhanVien nv = new NhanVien();
            nv.TenNV = txt_hoten.Text;
            nv.NgaySinh = DateTime.Parse(dtP_ngaysinh.Text);
            nv.DiaChi = txt_diachi.Text;
            nv.Gioitinh = cb_gioitinh.Text;
            nv.Cmnd = mtxt_cmnd.Text;
            nv.SoDienThoai = mtxt_sdt.Text;
            nv.NgayLam = DateTime.Parse(dtP_ngaylam.Text);
            nv.MaLoaiNV = nvService.LayMaLoaiNV(cb_loainv.Text);
            nv.MaNhaHang = nhService.LayMaNH(cb_nh.Text);
            if (nvService.ThemNhanVien(nv))
            {
                this.Close();
                MessageBox.Show("Thêm thành công");
                return;
            }
            else
            {
                MessageBox.Show("Lỗi");
                return;
            }
        }

        private void HienThiThongTinNV()
        {
            txt_hoten.Text = nv.TenNV;
            dtP_ngaysinh.Text = nv.NgaySinh.ToShortDateString();
            cb_gioitinh.Text = nv.Gioitinh;
            txt_diachi.Text = nv.DiaChi;
            mtxt_cmnd.Text = nv.Cmnd;
            mtxt_sdt.Text = nv.SoDienThoai;
            cb_loainv.Text = nvService.LayLoaiNV(nv.MaLoaiNV);
            dtP_ngaylam.Text = nv.NgayLam.ToShortDateString();
            cb_nh.Text = nhService.LayTenNH(nv.MaNhaHang);
        }

        private void frmThongTinNhanVien_Load(object sender, EventArgs e)
        {
            LoadGioiTinh();
            LoadNH();
            LoadLoaiNV();
            if (flag == 1)
            {
                btnThem.Visible = false;
                Point temp = new Point();
                temp.X = 11;
                temp.Y = 210;
                btnCapNhat.Location = temp;
                HienThiThongTinNV();
            }
        }
        bool kq =false;
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            nv.TenNV = txt_hoten.Text;
            nv.NgaySinh = DateTime.Parse(dtP_ngaysinh.Text);
            nv.Gioitinh = cb_gioitinh.Text;
            nv.DiaChi = txt_diachi.Text;
            nv.Cmnd = mtxt_cmnd.Text;
            nv.SoDienThoai=mtxt_sdt.Text;
            nv.MaLoaiNV = nvService.LayMaLoaiNV(cb_loainv.Text);
            nv.NgayLam = DateTime.Parse(dtP_ngaylam.Text);
            nv.MaNhaHang = nhService.LayMaNH(cb_nh.Text);
            
            if (fix && dirty)
                kq = nvService.CapNhatNVDirtyread(nv);
            if(!fix && dirty)
                kq = nvService.CapNhatNVDirtyreadError(nv);
            if(fix && lost)
                kq = nvService.CapNhatNVLostUpdate(nv);
            if(!fix && lost)
                kq = nvService.CapNhatNVLostUpdateError(nv);
            if (kq)
            {
                MessageBox.Show("Thành Công");
                this.Close();
                return;
            }
            else
            {
                MessageBox.Show("Thất bại");
                return;
            }
        }
    }
}
